/**
 * Created with CLion.
 * Description: 
 * User: loong
 * Date: 2025/4/18
 * Time: 22:31
 */

#ifndef GAME_H
#define GAME_H
#include "AssetStore.h"

#include <SDL3/SDL.h>
#include <glm/vec2.hpp>
#include <random>
#include <string>

struct Texture;
class Scene;
class Game
{
public:
    static Game & GetInstance()
    {
        static Game instance;
        return instance;
    }

    void Run();// 运行游戏, 执行游戏主循环

    void Init(const std::string & _title, int _width, int _height);// 初始化游戏

    void HandleEvents();  // 处理事件
    void Update(float dt);// 更新游戏状态
    void Render();        // 渲染游戏
    void Clean();         // 清理游戏资源

    void RenderTexture(const Texture & _texture, const glm::vec2 & _position, const glm::vec2 & _size);// 渲染纹理
    void RenderFillCircle(const glm::vec2 & _position, const glm::vec2 & _size, float _alpha);

    // 随机数函数More actions
    float      RandomFloat(float min, float max) { return std::uniform_real_distribution<float>(min, max)(gen_); }
    int        RandomInt(int min, int max) { return std::uniform_int_distribution<int>(min, max)(gen_); }
    glm::vec2  RandomVec2(const glm::vec2 & min, const glm::vec2 & max) { return glm::vec2(RandomFloat(min.x, max.x), RandomFloat(min.y, max.y)); }
    glm::ivec2 RandomIVec2(const glm::ivec2 & min, const glm::ivec2 & max) { return glm::ivec2(RandomInt(min.x, max.x), RandomInt(min.y, max.y)); }

    // 工具函数
    void DrawGrid(const glm::vec2 & _top_left, const glm::vec2 & _bottom_right, float _grid_width, SDL_FColor _color);
    void DrawBoundary(const glm::vec2 & _top_left, const glm::vec2 & _bottom_right, float _boundary_width, SDL_FColor _fcolor);// 绘制边界

    glm::vec2    GetScreenSize() const { return screen_size_; };
    Scene *      GetCurrentScene() const { return current_scene_; }
    AssetStore * GetAssetStore() const { return asset_store_; }

    glm::vec2            MousePosition() const { return mouse_position_; }
    SDL_MouseButtonFlags MouseButtons() const { return mouse_buttons_; }

private:
    Game() {}
    Game(const Game &) = delete;

    Game & operator=(const Game &) = delete;

private:
    glm::vec2    screen_size_ {0, 0};// 屏幕大小
    bool         is_running_ {true}; // 游戏是否运行
    Scene *      current_scene_ {nullptr};
    AssetStore * asset_store_ {nullptr};

    Uint64 FPS_ {60};       // 游戏帧率
    Uint64 frame_delay_ {0};// 帧延迟，单位ns
    float  dt_ {0.0f};      // 帧间隔

    SDL_Window *   window_ {nullptr};  // 窗口
    SDL_Renderer * renderer_ {nullptr};// 渲染器

    std::mt19937 gen_ = std::mt19937(std::random_device {}());

    glm::vec2            mouse_position_ {0};
    SDL_MouseButtonFlags mouse_buttons_ {0};
};

#endif//GAME_H
